package com.excellent.system.controller;

import com.excellent.common.annotation.Log;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.ShiroUtils;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysStuTutor;
import com.excellent.system.domain.SysTeaBase;
import com.excellent.system.domain.SysUserGroupInterview;
import com.excellent.system.domain.SysUserGroupMachine;
import com.excellent.system.service.ISysDictDataService;
import com.excellent.system.service.ISysStuTutorService;
import com.excellent.system.service.ISysTeaBaseService;
import com.excellent.system.service.ISysTeaStuService;
import com.excellent.system.service.ISysUserGroupService;
import com.excellent.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 教师管理Controller
 *
 * @author YYY
 * @date 2020-12-23
 */
@Controller
@RequestMapping("/system")
public class SysTeaBaseController extends BaseController
{
    private String prefix = "system/tea";

    @Autowired
    private ISysTeaBaseService sysTeaBaseService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysTeaStuService sysTeaStuService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISysStuTutorService sysStuTutorService;

    @Autowired
    private ISysUserGroupService sysUserGroupService;




    @GetMapping("/interviewTea")
    public String interviewTea()
    {
        return prefix + "/interviewTea";
    }

    @GetMapping("/machineTea")
    public String machineTea()
    {
        return prefix + "/machineTea";
    }

    @GetMapping("/tea")
    public String tea()
    {
        return prefix + "/tea";
    }
    /**
     * 查询教师管理列表
     */
    @PostMapping("/tea/list")
    @ResponseBody
    public TableDataInfo list(SysTeaBase sysTeaBase)
    {
        SysUser user = ShiroUtils.getSysUser();

        //判断是否是管理员
        if(user.isAdmin())
        {
            startPage();
            SysUser suser=new SysUser();
            List<SysTeaBase> list =sysUserService.selectSysTeaBaseList(suser);
            return getDataTable(list);
        }
        else {
            String cases=null;
            String cases2=null;
            //根据学生学号 选择导师表信息
            SysStuTutor sysStuTutor =sysStuTutorService.selectSysStuTutorById(user.getLoginName());
            if( null != sysStuTutor.getTea1Jno() && sysStuTutor.getTea1Jno().length()!=0)
            {
                cases=sysStuTutor.getTea1Jno();
            }
            if(null != sysStuTutor.getTea2Jno() && sysStuTutor.getTea2Jno().length()!=0 )
            {
                cases2=sysStuTutor.getTea2Jno();
            }
            SysUser suser=new SysUser();
            List<SysTeaBase> list =sysUserService.selectSysTeaBaseList(suser);
            List<SysTeaBase> listcopy = new ArrayList<>();
            int size =list.size();
            for (int i=0;i<size;i++){
                SysTeaBase sysTeaBase1 = list.get(i);
                //判断是否已选择
                if(sysTeaBase1.getTeaJno().equals(cases)||sysTeaBase1.getTeaJno().equals(cases2))
                {
                    continue;
                }
//                sysTeaStuService.selectSysTeaStuById(sysTeaBase1.getTeaJno());
                int count =sysTeaStuService.selectCountSysTeaStuById(sysTeaBase1.getTeaJno());
                String title = sysTeaBase1.getTeaTitle();
                String StitleCount = sysDictDataService.selectDictLabel("sys_tea_title", title);
                int titleCount=StitleCount.charAt(0)-'0';
                if(count<titleCount)
                {
                    listcopy.add(list.get(i));
                }
                //sysTitleService.selectSysTitleById(titleCount);
            }
            return getDataTable(listcopy);
        }

    }

    /**
     * 查询教师管理列表
     */
    @PostMapping("/tea/confirm")
    @ResponseBody
    public TableDataInfo confirm()
    {
        SysUser user = ShiroUtils.getSysUser();

        if(user.isAdmin())
        {
            List<SysTeaBase> list =new ArrayList<>();
            return getDataTable(list);
        }
        else {
            SysUser sysUser=new SysUser();
            startPage();
            SysStuTutor sysStuTutor = sysStuTutorService.selectSysStuTutorById(user.getLoginName());
            List<SysTeaBase> list= new ArrayList<>();
            if(sysStuTutor.getTeaJno()!=null&&sysStuTutor.getTeaJno().length()!=0){
                sysUser.setLoginName(sysStuTutor.getTeaJno());
                list=sysUserService.selectSysTeaBaseList(sysUser);
            }
            return getDataTable(list);
        }
    }
    /**
     * 查询教师管理列表
     */
    @PostMapping("/tea/chooseList")
    @ResponseBody
    public TableDataInfo chooseList(SysTeaBase sysTeaBase)
    {
        SysUser user = ShiroUtils.getSysUser();
        SysUser suser=new SysUser();
        startPage();
        if(user.isAdmin())
        {
            List<SysTeaBase> list =sysUserService.selectSysTeaBaseList(suser);
            return getDataTable(list);
        }
        else {
            SysStuTutor sysStuTutor = sysStuTutorService.selectSysStuTutorById(user.getLoginName());
            List<SysTeaBase> list= new ArrayList<>();
            List<SysTeaBase> list2=new ArrayList<>();
            if( null != sysStuTutor.getTea1Jno() && sysStuTutor.getTea1Jno().length()!=0){
                suser.setLoginName(sysStuTutor.getTea1Jno());
                list=sysUserService.selectSysTeaBaseList(suser);
            }
            if( null != sysStuTutor.getTea2Jno() && sysStuTutor.getTea2Jno().length()!=0){
                suser.setLoginName(sysStuTutor.getTea2Jno());
                list2= sysUserService.selectSysTeaBaseList(suser);
            }
            if(list2!=null){
                list.addAll(list2);
            }
            return getDataTable(list);
        }
    }

    /**
     * 导出教师管理列表
     */
    @Log(title = "教师管理", businessType = BusinessType.EXPORT)
    @PostMapping("/tea/export")
    @ResponseBody
    public AjaxResult export(SysTeaBase sysTeaBase)
    {
        List<SysTeaBase> list = sysTeaBaseService.selectSysTeaBaseList(sysTeaBase);
        ExcelUtil<SysTeaBase> util = new ExcelUtil<SysTeaBase>(SysTeaBase.class);
        return util.exportExcel(list, "tea");
    }

    /**
     * 新增教师管理
     */
    @GetMapping("/tea/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存教师管理
     */
    @Log(title = "教师管理", businessType = BusinessType.INSERT)
    @PostMapping("/tea/add")
    @ResponseBody
    public AjaxResult addSave(SysTeaBase sysTeaBase)
    {
        return toAjax(sysTeaBaseService.insertSysTeaBase(sysTeaBase));
    }

    /**
     * 修改教师管理
     */
    @GetMapping("/machineTea/edit/{userId}")
    public String machineTeaEdit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("SysUser", sysUserService.selectUserById(userId));
        return prefix + "/machineTeaEdit";
    }
    /**
     * 修改教师管理
     */
    @GetMapping("/interviewTea/edit/{userId}")
    public String interviewTeaEdit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("SysUser", sysUserService.selectUserById(userId));
        return prefix + "/interviewTeaEdit";
    }

    /**
     * 修改保存教师管理
     */
    @Log(title = "教师管理", businessType = BusinessType.UPDATE)
    @PostMapping("/tea/machineTeaEdit")
    @ResponseBody
    public AjaxResult machineTeaEdit(SysUser user)
    {
        SysUserGroupMachine sysUserGroupMachine =new SysUserGroupMachine();
        sysUserGroupMachine.setUserId(user.getUserId().toString());
        sysUserGroupMachine.setGroupMachineId(user.getgroupMachineId());
        if(null ==sysUserGroupService.selectSysUserGroupMachineById(sysUserGroupMachine.getUserId())){
            return toAjax(sysUserGroupService.insertSysUserGroupMachine(sysUserGroupMachine));
        }else{
            return toAjax(sysUserGroupService.updateSysUserGroupMachine(sysUserGroupMachine));
        }
    }

    /**
     * 修改保存教师管理
     */
    @Log(title = "教师管理", businessType = BusinessType.UPDATE)
    @PostMapping("/tea/interviewTeaEdit")
    @ResponseBody
    public AjaxResult interviewTeaEdit(SysUser user)
    {
        SysUserGroupInterview sysUserGroupInterview =new SysUserGroupInterview();
        sysUserGroupInterview.setUserId(user.getUserId().toString());
        sysUserGroupInterview.setGroupInterviewId(user.getgroupInterviewId());
        if(null ==sysUserGroupService.selectSysUserGroupInterviewById(sysUserGroupInterview.getUserId())){
            return toAjax(sysUserGroupService.insertSysUserGroupInterview(sysUserGroupInterview));
        }else{
            return toAjax(sysUserGroupService.updateSysUserGroupInterview(sysUserGroupInterview));
        }

    }

    /**
     * 删除教师管理
     */
    @Log(title = "教师管理", businessType = BusinessType.DELETE)
    @PostMapping( "/tea/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTeaBaseService.deleteSysTeaBaseByIds(ids));
    }
}


